﻿<?php
//Establish Session
/**
 * @author Invisible Man
 * @copyright 2013
 */
 //Checking Admin
 require('../includes/admin_auth.php');
 //Check data input
 if (!isset($_GET['id'])) {
    header("location: cate_list.php");
    exit();
 }
 else {
    $id = $_GET['id'];
 }
//Callback all pages
require('../config.php');
//Before Enable
$page_title ='Trang chủ';
$menu_ex = array("cate_list.php" => "Danh sách Danh mục");
//Starting all pages
//Callback header
require('theme/default/header.php');
//Callback Content  
//Connect Database
require('../includes/db_connect.php');
//Query
$query_info = 'SELECT * FROM categories WHERE cate_id=' . $id;
$data_info = mysql_query($query_info, $db_con);
if (mysql_num_rows($data_info) != 0) {
    $data_value = mysql_fetch_assoc($data_info);
}
//Checking input
if (isset($_POST['btnEdit'])) {
    //Checking Empty
    if (empty($_POST['txtCate'])) {
        echo '<p class="error_msg">Bạn chưa nhập tên Danh mục</p>';
    } elseif ($_POST['txtOrder'] < 0) {
        echo '<p class="error_msg">Thứ tự sắp xếp phải lớn hơn hoặc bằng 0</p>';
    } else { 
        if (mysql_num_rows($data_info) == 0) {
            echo '<p class="error_msg">Không tìm thấy dữ liệu</p>';
        } else {  
            $cate = addslashes($_POST['txtCate']);
            $order = (int)$_POST['txtOrder'];      
                if ($order == "" || $order <= 0 || is_numeric($order) == false) {
                    $order = 1;
                }
                $query_info = 'UPDATE categories SET cate_name="' .$cate. '", cate_order=' . $order . ' WHERE cate_id='.$id;
                mysql_query($query_info, $db_con);
                header("location: cate_list.php");
                exit();   

        }
    }
}
//FORM
echo '<form action="'.$_SERVER['PHP_SELF'].'?id=' .$id. '" method="post" style="width: 650px; margin: 30px auto;">
    <fieldset>
        <legend>Sửa Danh mục</legend>
        <table>
            <tr>
                <td></td>
                <td>
                    <span class="form_label">Danh mục:</span>
                    <span class="form_item"><input type="text" name="txtCate" class="textbox" value="' .htmlspecialchars($data_value['cate_name']). '" maxlength="100" /></span><br />
                    <span class="form_label">Thứ tự:</span>
                    <span class="form_item"><input type="text" name="txtOrder" class="textbox" value="' .$data_value['cate_order']. '" maxlength="3" /></span><br />
                    <span class="form_label"></span>
                    <span class="form_item"><input type="submit" name="btnEdit" value="Sửa Danh mục" class="button"/></span><br />
                </td>
            </tr>
        </table>
    </fieldset>
</form>';
//Callback Footer
require('theme/default/footer.php');
?>

